//
//  NSString+Helper.m
//  itboye2015002
//
//  Created by boye_mac1 on 15/8/18.
//  Copyright (c) 2015年 Boye. All rights reserved.
//

#import "NSString+Helper.h"

@implementation NSString (Helper)

-(NSString *)stringByDecodingURLFormat
{
    NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
    result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return result;
}

-(NSString *)stringByEncodingURLFormat
{
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              (CFStringRef)self,
                                                              (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
                                                              NULL,
                                                              kCFStringEncodingUTF8));
    
    return encodedString;
}

// 是否为空

-(BOOL) isStringNull{
    
    if (self == nil|| self.length <=0 || [self class] == [NSNull class]) {
        return YES;
    }
    return NO;
}

#pragma mark -- 字符串相等 ---
-(BOOL)isEqualToStr:(NSString *)string{
    
    if ([self isEqualToString:string]) {
        return YES;
    }else{
        return NO;
    }
}


#pragma mark -- 邮箱验证  ---
-(BOOL) checkEmail{
    
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}


#pragma mark -- 手机号验证  ---
- (BOOL)checkMobile{
    //    NSString *regex = @"[0-9]{11}";
//    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
//    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
//    BOOL isMatch = [pred evaluateWithObject:self];
//    return isMatch;
    NSString * mobileRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    
//    NSString *mobileRegex = @"^((13[0-9])|(147)|(170)|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileRegex];
    BOOL isMatch = [mobileTest evaluateWithObject:self];
    return  isMatch;
    
}

//
//- (BOOL)isMobileNumber:(NSString *)mobileNum
//{
//    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
//    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
//    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
//    NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
//    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
//    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
//    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
//    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
//    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
//    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
//        || ([regextestcm evaluateWithObject:mobileNum] == YES)
//        || ([regextestct evaluateWithObject:mobileNum] == YES)
//        || ([regextestcu evaluateWithObject:mobileNum] == YES))
//    {
//        return YES;
//    }
//    else
//    {
//        return NO;
//    }
//}


#pragma mark --正则匹配 ---

-(BOOL)checkPredicateStr:(NSString *)regex{
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:self];
    return isMatch;
}

#pragma mark -- 根据某个字符分割字符串  ---
-(NSArray *)separatedByString:(NSString *)string{
    NSMutableString * mutablestr = [[NSMutableString alloc] initWithString:self];
    NSArray * array = [mutablestr componentsSeparatedByString:string];
    return array;
}

#pragma mark  获得字符串的大小（size）
-(CGSize)getSizeOfString:(float) font{
   
    UIFont* infofont = FONT(font);
    //CGSize size = [info sizeWithFont:font];//这个字体大小一定要和label的字体大小保持一致
    CGSize size = [self sizeWithAttributes:@{NSFontAttributeName: infofont}];
    return size;
}

#pragma mark  获得固定宽度字符串的大小（size）

-(CGRect) getBoundingRectOfStringSize:(CGSize)size font:(float) font{
    
    NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
    pStyle.lineBreakMode = NSLineBreakByCharWrapping;
    //修改代码
    CGRect rect = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:font],NSParagraphStyleAttributeName:pStyle} context:nil];
    
    return rect;
}

#pragma mark --  改变指定位置字符串属性
-(NSMutableAttributedString * ) changeText:(UIColor *) color range:(NSRange) range{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self];
    
    //设置颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range]; // 0为起始位置 length是从起始位置开始 设置指定颜色的长度
    return  attributedString;
    
}

#pragma mark -- Label删除线 --
-(NSMutableAttributedString *)deleteLabelAttribut{
    
    return [self deleteLabelAttribut:[UIColor colorWithHexString:@"#abaaa8"] range:[self rangeOfString:self]];
}

#pragma mark -- 自定义颜色和位置的删除线 --
-(NSMutableAttributedString *)deleteLabelAttribut:(UIColor *) color range:(NSRange )range{
  
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self];

    [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:range];//删除线
    [attrString addAttribute:NSStrikethroughColorAttributeName value:color range:range];//删除线蓝色
    
    return attrString;
}

#pragma mark -- 字符串是否包含另一个字符串 --
-(BOOL)stringIsContenString:(NSString *)otherString{
    
    if([self rangeOfString:otherString].location != NSNotFound) {
        return YES;

    } else {
        return NO;
    }
    
    
    
//    NSString * string = [NSString c];
}

//
//-(NSString *) getStringArray:(NSArray *)array :(NSString *)separeStr{
//    
//    NSString  * string = array[0];
//    for (NSString * sonstr in array) {
//        string = [NSString stringWithFormat:@"%@%@%@",string,separeStr,sonstr];
//    }
//    return string;
//}


#pragma mark -- zk
- (NSString *)getEncryptedString {
    
    if (self.length == 11) {
        return [self stringByReplacingCharactersInRange:NSMakeRange(4, 4) withString:@"****"];
    }
    else if (self.length == 18) {
        return [self stringByReplacingCharactersInRange:NSMakeRange(8, 6) withString:@"******"];
    }
    else {
        return [self stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"*"];
    }
    
}

@end
